/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.iec.edp.caf.commons.exception.diagnostic.analyzer;

import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
import org.springframework.boot.diagnostics.FailureAnalysis;

import java.io.FileNotFoundException;
import java.util.LinkedHashMap;

/**
 * 找不到类型错误分析器
 * @author wangyandong
 */
public class ClassNotFoundFailureAnalyzer  extends AbstractFailureAnalyzer<ClassNotFoundException> {
    private static final LinkedHashMap<String,String> KeyApps = new LinkedHashMap<>();
    public ClassNotFoundFailureAnalyzer(){
        super();
        KeyApps.put("ARAP","应收应付");
        KeyApps.put("BA","BA");
        KeyApps.put("BF","基础数据");
        KeyApps.put("BP","业务中台");
        KeyApps.put("CT","合同管理");
        KeyApps.put("SASA","企业大脑");
        KeyApps.put("EAM","资产管理");
        KeyApps.put("EIS","电子影像");
        KeyApps.put("ES","电子采购");
        KeyApps.put("ERM","电子档案");
        KeyApps.put("FI","财务会计");
        KeyApps.put("FSSP","财务共享");
        KeyApps.put("IHC","资金池");
        KeyApps.put("IPS","投资管理");
        KeyApps.put("MA","管理会计");
        KeyApps.put("MDM","主数据管理");
        KeyApps.put("PS","项目管理");
        KeyApps.put("QM","质量管理");
        KeyApps.put("SCM","供应链");
        KeyApps.put("TAX","税务管理");
        KeyApps.put("TM","资金管理");
    }
    @Override
    protected FailureAnalysis analyze(Throwable rootFailure, ClassNotFoundException cause) {

        String className = cause.getMessage().toLowerCase();
        String action = "";
        if(className.startsWith("io.iec.edp") ||
                className.startsWith("com.inspur.edp") ||
                className.startsWith("com.inspur.igix")){
            action = "请联系【平台部】处理";
        }else if(className.startsWith("com.inspur.gs")){
            String[] names = className.split(".");
            if(names.length>=4){
                String code = names[3].toUpperCase();
                if(KeyApps.containsKey(code)) {
                    action = String.format("请联系【%s】处理！", KeyApps.get(code));
                }else{
                    action = "请联系【产品部】处理！";
                }
            }else{
                action = "请联系【产品部】处理！";
            }
        }else if(className.startsWith("inspur.cb")){
            action = "请联系【预算管理】处理！";
        }
        return new FailureAnalysis("缺少类型：" + cause.getMessage(),
                action,
                rootFailure);
    }
}